package com.rf.business.control.demo;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rf.business.model.demo.Demo;
import com.rf.business.service.demo.DemoService;
import com.rf.sys.frm.BaseController;
import com.rf.sys.frm.JsonDateValueProcessor;
import com.rf.sys.utils.JsonUtils;
import com.rf.sys.utils.PageUtils;


@Controller
@RequestMapping("/demo/demo")
public class DemoController extends BaseController{
	/**
	 * 日志
	 */
	private static Logger log = LoggerFactory.getLogger(DemoController.class);
	private static final long serialVersionUID = -120161020102459972L;
	
	@Resource
	private DemoService deomService;
	
	/**
	 * 初始化页面信息
	 * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
	 */
	@RequestMapping(value = "index")
    public ModelAndView index (HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/admin/Demo/list");
		return modelAndView; 
    }
    
    /**
	 * 搜索页面信息
	 * @param Page,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "query")
    @ResponseBody
    public void query (PageUtils pageUtils, Demo Demo, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	//获取分页、查询参数，进行数据封装
    	Map searchMap = deomService.getDemoPage(Demo, pageUtils);
    	super.responsePrint(request, response, JsonUtils.mapToJson(searchMap));
    }
    
    /**
     * 编辑页面初始化
     * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "edit")
    public void edit (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	JSONObject returnObj = new JSONObject();
		String id = request.getParameter("id");
		if(id != null && !id.equals("")){
			Demo Demo = deomService.getDemoByID(id);
//			JSONObject jsonObject = JSONObject.fromObject( Demo ); 
			JsonConfig jsonConfig = new JsonConfig();    
		    jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor());  
		    JSONObject Demojson = JSONObject.fromObject( Demo,jsonConfig );
			
			returnObj.element("data", Demojson);//成功
		}
		returnObj.element("httpCode", "OK");//成功
		super.responsePrint(request, response, returnObj.toString());
    }
    
    /**
     * 保存数据信息
     * @param Demo,request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "save")
    public Object save ( Demo Demo,HttpServletRequest request, HttpServletResponse response ) throws Exception{
//    	String params=request.getParameter("data");
//    	JSONObject jsonObject = JSONObject.fromObject(params);
//    	Demo Demo  = (Demo) JSONObject.toBean(jsonObject, Demo.class);    
    	if(Demo !=null && Demo.getId()!=null && !Demo.getId().equals("")){
			//修改
			deomService.updateDemo(Demo);
		}else{
			//新增
			//Demo.setCreateTime(new Date());
			deomService.addDemo(Demo);
		}
//    	if(a==1){
//			throw new BusinessException("数据XXXX录入不正确！！");//业务异常
//		}else{
//			throw new EquipException("摄像头异常！");//设备异常
//		}
		ModelMap modelMap=new ModelMap();
		
		modelMap.put("httpCode", "OK");
		modelMap.put("timestamp", System.currentTimeMillis());
		return ResponseEntity.ok(modelMap);
    }
    
    /**
     * 删除数据信息
     * @param Demo,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "delete")
    @ResponseBody
    public void delete (Demo Demo, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String id=request.getParameter("id");
    	//Demo.setStats("100104");//删除状态
    	deomService.deleteDemo(id);
		returnObj.element("httpCode", "OK");//成功
		super.responsePrint(request, response, returnObj.toString());
    }
    
    
    /**
     * 根据项目下拉
     * @param request
     * @param response
     * @param modelMap
     * @throws Exception 
     */
	@RequestMapping(value = "queryDemoDic", method = RequestMethod.GET)
	@ResponseBody
	public void queryDemoDic(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Demo> list = deomService.getDemo();
		Demo sysdic=new Demo();
		sysdic.setId("-1");
		sysdic.setName("请选择...");
		list.add(sysdic);
		JSONArray jsonarray = JSONArray.fromObject(list);  
		responsePrint(request,response, jsonarray.toString());
	}
}


